package com.masterclient.app.businessmodule.homemodule.presenter.sign;

import android.text.TextUtils;

import com.eoner.baselib.utils.sp.SPUtils;
import com.masterclient.app.businessmodule.homemodule.bean.sign.HomeSignBean;
import com.google.gson.Gson;

public class HomeSignManager {

    private static final String KEY_SIGN = "home_key_sign";

    /**
     * 保存签名信息
     * */
    public void updateMySignData(HomeSignBean signBean) {
        signBean.setCurrentTime(System.currentTimeMillis());
        String json = new Gson().toJson(signBean);
        SPUtils.getInstance().put(KEY_SIGN,json);
    }

    /**
    * 缓存是否有效
    * */
    //TODO:暂不处理
    public boolean isCacheEffective() {
        String json = SPUtils.getInstance().getString(KEY_SIGN,"");
        if (!TextUtils.isEmpty(json)) {
            HomeSignBean signBean = new Gson().fromJson(json,HomeSignBean.class);
            if (System.currentTimeMillis() - signBean.getCurrentTime() >= 1000*60*60*24) {
                //如果大于24小时，失效 (清空)
                SPUtils.getInstance().put(KEY_SIGN,"");
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    public HomeSignBean getSignData() {
        String json = SPUtils.getInstance().getString(KEY_SIGN,"");
        if (!TextUtils.isEmpty(json)) {
            HomeSignBean signBean = new Gson().fromJson(json,HomeSignBean.class);
            return signBean;
        }
        return null;
    }

}
